package com.example.cookbook.activity;


import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;


import com.example.cookbook.MyDBHelper;
import com.example.cookbook.R;
import com.example.cookbook.adapter.StepAdapter;
import com.example.cookbook.adapter.YlAdapter;
import com.example.cookbook.bean.RecipeBean;
import com.example.cookbook.bean.StepsBean;
import com.example.cookbook.bean.YlBean;

import java.util.ArrayList;
import java.util.List;

//菜谱详情界面
public class RecipeDetailActivity extends AppCompatActivity {
    //定义
    RecyclerView recyclerView_yl;          //食谱用料列表
    RecyclerView recyclerView_step;       //做法列表
    RecipeBean bean;                            //存放上一个界面传递过来的参数

    //收藏功能
    Button btn_favorite;        //收藏按钮
    boolean isFavorite=false;   //表示数据条是否已保存
    MyDBHelper dbHelper;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recipe_detail);
        //绑定控件
        initView();
        //更新数据
        loadData();
    }
    //绑定控件
    private void initView() {
        recyclerView_yl=findViewById(R.id.recy_view_yl);
        recyclerView_step=findViewById(R.id.recy_view_step);
        bean=(RecipeBean) getIntent().getSerializableExtra("data");

        //收藏功能
        dbHelper=new MyDBHelper(this);
        btn_favorite=findViewById(R.id.btn_favorite);
        btn_favorite.setOnClickListener(v -> {
            if (isFavorite){            //如果已经收藏
                dbHelper.getWritableDatabase().execSQL("DELETE FROM tb_recipe WHERE recipeId=?",new Object[]{bean.getId()});
            }else {
                dbHelper.getWritableDatabase().execSQL("INSERT INTO tb_recipe(recipeId,recipeName,recipeDesc,recipeSmallImgUrl) VALUES (?,?,?,?)",new Object[]{bean.getId(),bean.getCpName(),bean.getDes(),bean.getSmallImg()});
            }
            isFavorite=!isFavorite;
            btn_favorite.setText(isFavorite?"取消收藏":"收藏");
        });

    }
    //更新数据
    private void loadData() {
        List<YlBean> arrlistyl = new ArrayList<>(bean.getYl());
        List<StepsBean> arrliststep = new ArrayList<>(bean.getSteps());

        YlAdapter adapteryl = new YlAdapter(this, arrlistyl);
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
        recyclerView_yl.setLayoutManager(layoutManager);
        recyclerView_yl.setAdapter(adapteryl);

        StepAdapter adapterstep = new StepAdapter(this, arrliststep);
        StaggeredGridLayoutManager layoutManager1 = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
        recyclerView_step.setLayoutManager(layoutManager1);
        recyclerView_step.setAdapter(adapterstep);

        // 收藏功能的实现
        Cursor cursor = null;
        try {
            cursor = dbHelper.getReadableDatabase().rawQuery("SELECT id FROM tb_recipe WHERE recipeId=?", new String[]{bean.getId()});
            isFavorite = cursor != null && cursor.getCount() > 0;
        } catch (Exception e) {
            Log.e("TAG", "Error querying favorite status: " + e.getMessage());
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }

        btn_favorite.setText(isFavorite ? "取消收藏" : "收藏");
    }

}
